package edu.rmit.client;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import edu.rmit.contant.ServerServiceConstant;
import edu.rmit.service.IChatService;
import edu.rmit.service.IGroupService;
import edu.rmit.service.IMessageService;
import edu.rmit.service.ISystemInfoService;
import edu.rmit.service.IUserService;

/**
 * This class is to implement the group operation. Like get groups by status,
 * delete group, and change group status function.
 * 
 * @author Thaw Zin
 * @version 1.0
 */
public class Client { 
	/**
	 * The server address which provide the RMI service.
	 */
	private String serverName = "127.0.0.1";

	/**
	 * The server port which provide the RMI service.
	 */
	private int port = 1099;

	/**
	 * This instance provide the user related service.
	 */
	private IUserService userService;

	/**
	 * This instance provide the system info related service.
	 */
	private ISystemInfoService systemService;

	/**
	 * This instance provide the group related service.
	 */
	private IGroupService groupService;

	/**
	 * This instance provide the chat related service.
	 */
	private IChatService chatService;

	/**
	 * This instance provide the message related service.
	 */
	private IMessageService messageService;

	/**
	 * The constructor without parameter.
	 */
	public Client() {
	}

	/**
	 * The constructor provide the server address and port.
	 * 
	 * @param serverName
	 *            The server address which provide the RMI service
	 * @param port
	 *            The server port which provide the RMI service.
	 */
	public Client(String serverName, int port) {
		this.serverName = serverName;
		this.port = port;
	}

	/**
	 * This method is to look up the service provided the RMI service.
	 */
	public void lookUpService() {
		try {
			userService = (IUserService) Naming.lookup("rmi://" + serverName
					+ ":" + Integer.toString(port) + "/"
					+ ServerServiceConstant.USER_INFO_SERVICE);

			systemService = (ISystemInfoService) Naming.lookup("rmi://"
					+ serverName + ":" + Integer.toString(port) + "/"
					+ ServerServiceConstant.SYSTEM_INFO_SERVICE);

			groupService = (IGroupService) Naming.lookup("rmi://" + serverName
					+ ":" + Integer.toString(port) + "/"
					+ ServerServiceConstant.GROUP_INFO_SERVICE);

			chatService = (IChatService) Naming.lookup("rmi://" + serverName
					+ ":" + Integer.toString(port) + "/"
					+ ServerServiceConstant.CHAT_SERVER_SERVICE);

			messageService = (IMessageService) Naming.lookup("rmi://"
					+ serverName + ":" + Integer.toString(port) + "/"
					+ ServerServiceConstant.MESSAGE_INFO_SERVICE);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Get the server address.
	 * @return The server address.
	 */
	public String getServerName() {
		return serverName;
	}

	/**
	 * Set the server address.
	 * @param serverName The server address which is to be setted.
	 */
	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	/**
	 * Get the server port.
	 * @return The server port which provide the RMI service.
	 */
	public int getPort() {
		return port;
	}

	/**
	 * Set the server port.
	 * @param port The server port which provide the RMI service.
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * Get the user related service.
	 * @return The user related service.
	 */
	public IUserService getUserService() {
		return userService;
	}

	/**
	 * Get the system info related service.
	 * @return The system info related service.
	 */
	public ISystemInfoService getSystemService() {
		return systemService;
	}

	/**
	 * Get the group related service.
	 * @return The group related service.
	 */
	public IGroupService getGroupService() {
		return groupService;
	}

	/**
	 * Get the chat related service.
	 * @return The chat related service.
	 */
	public IChatService getChatService() {
		return chatService;
	}

	/**
	 * Get the message related service.
	 * @return The message related service.
	 */
	public IMessageService getMessageService() {
		return messageService;
	}
}
